﻿using System;
using System.ComponentModel;
using System.Runtime.CompilerServices;

namespace BoringTodo
{
    // 定义任务类型枚举
    public enum TaskType
    {
        UP类 = 0,
        jump类 = 1,
        其他类 = 2,
        有价值工作 = 3,
        杂活 = 4
    }

    public class TodoItem : INotifyPropertyChanged, IDataErrorInfo
    {
        private string _description;
        private string _notes;
        private DateTime? _creationDate;
        private DateTime? _completionDate;
        private TaskType _taskType = TaskType.其他类; // 默认为"其他类"

        // Using Chinese property names for backward compatibility with UI
        public string 描述
        { 
            get => _description; 
            set 
            { 
                if (_description != value)
                {
                    _description = value;
                    OnPropertyChanged();
                    OnPropertyChanged(nameof(Description));
                }
            } 
        }

        public string 备注
        { 
            get => _notes; 
            set 
            { 
                if (_notes != value)
                {
                    _notes = value;
                    OnPropertyChanged();
                    OnPropertyChanged(nameof(Notes));
                }
            } 
        }

        public DateTime? 预期完成时间
        { 
            get => _creationDate; 
            set 
            { 
                if (_creationDate != value)
                {
                    _creationDate = value;
                    OnPropertyChanged();
                    OnPropertyChanged(nameof(CreationDate));
                }
            } 
        }

        public DateTime? 完成时间
        { 
            get => _completionDate; 
            set 
            { 
                if (_completionDate != value)
                {
                    _completionDate = value;
                    OnPropertyChanged();
                    OnPropertyChanged(nameof(CompletionDate));
                }
            } 
        }

        public TaskType 任务类型
        {
            get => _taskType;
            set
            {
                if (_taskType != value)
                {
                    _taskType = value;
                    OnPropertyChanged();
                    OnPropertyChanged(nameof(TaskTypeValue));
                }
            }
        }

        // English property names for code readability
        public string Description
        {
            get => _description;
            set
            {
                if (_description != value)
                {
                    _description = value;
                    OnPropertyChanged();
                    OnPropertyChanged(nameof(描述));
                }
            }
        }

        public string Notes
        {
            get => _notes;
            set
            {
                if (_notes != value)
                {
                    _notes = value;
                    OnPropertyChanged();
                    OnPropertyChanged(nameof(备注));
                }
            }
        }

        public DateTime? CreationDate
        {
            get => _creationDate;
            set
            {
                if (_creationDate != value)
                {
                    _creationDate = value;
                    OnPropertyChanged();
                    OnPropertyChanged(nameof(预期完成时间));
                }
            }
        }

        public DateTime? CompletionDate
        {
            get => _completionDate;
            set
            {
                if (_completionDate != value)
                {
                    _completionDate = value;
                    OnPropertyChanged();
                    OnPropertyChanged(nameof(完成时间));
                }
            }
        }

        public TaskType TaskTypeValue
        {
            get => _taskType;
            set
            {
                if (_taskType != value)
                {
                    _taskType = value;
                    OnPropertyChanged();
                    OnPropertyChanged(nameof(任务类型));
                }
            }
        }

        public bool IsCompleted => CompletionDate.HasValue;

        // Implement IDataErrorInfo for validation
        public string Error => null;

        public string this[string propertyName]
        {
            get
            {
                string error = null;

                switch (propertyName)
                {
                    case nameof(Description):
                    case nameof(描述):
                        if (string.IsNullOrWhiteSpace(_description))
                        {
                            error = "任务描述不能为空";
                        }
                        else if (_description.Length > 200)
                        {
                            error = "任务描述不能超过200个字符";
                        }
                        break;

                    case nameof(CreationDate):
                    case nameof(预期完成时间):
                        if (!_creationDate.HasValue)
                        {
                            error = "创建时间不能为空";
                        }
                        break;
                }

                return error;
            }
        }

        public event PropertyChangedEventHandler PropertyChanged;

        protected void OnPropertyChanged([CallerMemberName] string propertyName = "")
        {
            PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
        }

        public override string ToString()
        {
            return Description;
        }
    }
}

